﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SchoolBusiness;
using DataModel;
using System.Globalization;
using System.IO;

namespace SchoolAdministration
{
    public partial class EditStudent : System.Web.UI.Page
    {
        private readonly Student blstd = new Student();
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                PopulateDateofBirth();
                LoadStudentData();
                        }

        }
        public void LoadStudentData()
        {
            MStudent _std = new MStudent();
            int studentId = int.Parse(Request.QueryString["StudentId"]);
            _std = blstd.GetStudentById(studentId);
            string name = Request.QueryString["Student"];
            TxtStudentFName.Attributes.Add("Value", name);
            TxtMiddleName.Attributes.Add("Value", _std.MiddleName);
            TxtStudentLName.Attributes.Add("Value", _std.LastName);
            TxtSection.Attributes.Add("Value", _std.section);
            TxtRoll.Attributes.Add("Value", _std.Roll.ToString());
            StudentAddress.Attributes.Add("Value", _std.Address);
            StuMobile.Attributes.Add("Value", _std.Mobile.ToString());
            ddlYear.ClearSelection();
            ddlYear.Items.FindByValue(_std.DOB.Year.ToString()).Selected = true;
            //ddlYear.Attributes.Add("Value", _std.DOB.ToString("YYYY"));
            ddlMonth.ClearSelection();
            ddlMonth.Items.FindByValue(_std.DOB.Month.ToString()).Selected = true;
            ddlDays.ClearSelection();
            ddlDays.Items.FindByValue(_std.DOB.Day.ToString()).Selected = true;
            ParentFName.Attributes.Add("Value", _std.Parent.FirstName);
            ParentMName.Attributes.Add("Value", _std.Parent.MiddleName);
            ParentLName.Attributes.Add("Value", _std.Parent.LastName);
            ParentPhone.Attributes.Add("Value", _std.Parent.Phone.ToString());
            ParentAddress.Attributes.Add("Value", _std.Parent.Address);

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            MStudent student = new MStudent()
            {
                FirstName = TxtStudentFName.Text,
                LastName = TxtStudentLName.Text,
                MiddleName = TxtMiddleName.Text,
                Roll = Convert.ToInt32(TxtRoll.Text),
                Address = StudentAddress.Text,
                Mobile=StuMobile.Text,
                DOB = Convert.ToDateTime(ddlYear.Text + "-" + ddlMonth.SelectedValue.ToString() + "-" + ddlDays.Text),
                Grade = Convert.ToInt32(Grade.Text),
                section = TxtSection.Text,
                Hostel = ChkHostel.Checked,
                Transport = ChkTransport.Checked,
                Parent = new StudentParent()
                {
                    FirstName = ParentFName.Text,
                    MiddleName = ParentMName.Text,
                    LastName = ParentLName.Text,
                    Address = ParentAddress.Text,
                    Phone = ParentPhone.Text
                },
            };
             Images img=new Images();
             if (fupPhoto.HasFile)
             {
                 try
                 {
                     string[] allowdFile = { ".png", ".jpeg", "jpg", ".gif", ".bmp" };
                     string FileExt = System.IO.Path.GetExtension(fupPhoto.PostedFile.FileName);
                     bool isValidFile = allowdFile.Contains(FileExt);
                     if (!isValidFile)
                     {
                         // lblerror.ForeColor = System.Drawing.Color.Red;
                         //lblerror.Text = "Please image only ";
                     }
                     else
                     {
                         if (fupPhoto.PostedFile.ContentLength <= 102400)
                         {
                             img.PhotoName = fupPhoto.FileName;
                             string path = Server.MapPath("StudentPhoto/" + img.PhotoName);
                             fupPhoto.SaveAs(path);
                             FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);
                             BinaryReader br = new BinaryReader(fs);
                             img.Photo = br.ReadBytes(Convert.ToInt32(fs.Length));
                             br.Close();
                             fs.Close();
                             int getPos = path.LastIndexOf("\\");
                             int len = path.Length;
                             string getPath = path.Substring(getPos, len - getPos);
                             string pathToStore = getPath.Remove(0, 1);
                             img.PhotoPath = "~/StudentPhoto/" + pathToStore;
                         }
                     }
                 }
                 catch (Exception ex)
                 {
                     //lblerror.Text = "Error occurred while uploading a file";
                 }
             }
            int result = blstd.addstudent(student);
            if (result > 1)
            {
                MsgDisplaylbl.Text = "Successfully entered Student";
                ClearAll();
            }
        }

        private void PopulateDateofBirth()
        {
            for (int i = 1970; i <= 2020; i++)
            {
                ddlYear.Items.Add(i.ToString());
            }
            ddlYear.Items.FindByValue(System.DateTime.Now.Year.ToString()).Selected = true;

            DateTimeFormatInfo info = DateTimeFormatInfo.GetInstance(null);

            for (int i = 1; i < 13; i++)
            {
                ddlMonth.Items.Add(new ListItem(info.GetMonthName(i), i.ToString()));

            }
            FillDays();
        }

        public void FillDays()
        {
            ddlDays.Items.Clear();
            int noofdays = DateTime.DaysInMonth(Convert.ToInt32(ddlYear.SelectedValue), Convert.ToInt32(ddlMonth.SelectedValue));
            for (int i = 1; i <= noofdays; i++)
            {
                ddlDays.Items.Add(i.ToString());
            }
            ddlDays.Items.FindByValue(System.DateTime.Now.Day.ToString()).Selected = true;
        }

        protected void ddlMonth_SelectedIndexChanged(object sender, EventArgs e)
        {
            FillDays();
        }
       
        public void ClearAll()
        {

            TxtStudentFName.Text = "";
            TxtMiddleName.Text = "";
            TxtStudentLName.Text = "";
            TxtRoll.Text = "";
            TxtSection.Text = "";
            Grade.Text = "";
            ParentFName.Text = "";
            ParentLName.Text = "";
            ParentMName.Text = "";
            ParentMobile.Text = "";
            ParentPhone.Text = "";
            ParentAddress.Text = "";
        }

        protected void ddlYear_SelectedIndexChanged(object sender, EventArgs e)
        {
            FillDays();
        }

    }
}